//
//  main.m
//  Example_Block
//
//  Created by 蒋浩源 on 15/7/19.
//  Copyright (c) 2015年 自学教程. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Example_Block-swift.h"

typedef long(^BlkSum)(int a,int b);

BlkSum sum = ^long (int a,int b){// global block ，在text段
    return a+b;
};

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");
        SW_Helloworld *swHelloObj = [[SW_Helloworld alloc] init];
        NSString *sentence = [swHelloObj sayHello:@"we are the world." withName:@"蒋"];
        NSLog(@"sentence : %@",sentence);
        
        NSLog(@"text block sum: %ld",sum(99, 11));
        
        //栈中的block
        int c = 10;
        NSString *name = @"seasun";
        BlkSum sumStack = ^long (int a,int b){
//            NSLog(@"c : %d",c+a+b);
            NSLog(@"name : %@", name);
            return c+a+b;
        };
        c ++;
        name = @"seasunk";
        NSLog(@"编译期已c变量的值copy到block里，所以后面再改变c的变量，也不影响sumStack的调用结果。");
        NSLog(@"栈中的block：%ld \n=name:%@=c的值：%d",sumStack(1,2),name, c);
        
        
//        NSString *text = @"工7月sd2f67颗金豆";
//        NSError *error = nil;
//        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^.*[^0-9](\\d+)颗.*" options:0 error:&error];
//        
//        if (regex) {
//            NSTextCheckingResult *firstMatch = [regex firstMatchInString:text options:0 range:NSMakeRange(0, text.length)];
//            NSRange matchRange = [firstMatch rangeAtIndex:1];
//            NSString *points = [text substringWithRange:matchRange];
//            NSInteger point = [points integerValue];
//        }
    }
    
    
    //堆中的block
//    BlkSum mallocBlock = [[sumStack copy] autorelease];
    

    return 0;
}
